import { ECSEntity } from "../../../3rd/ECS/ECSEntity";
import { ECSSystem } from "../../../3rd/ECS/ECSSystem";
import { BindNode } from "../../../Bridge/BindNode";
import { Vector3 } from "../../../Bridge/Vec";
import Util from "../../../Framework/Util/Util";
import { GameRT } from "../../GameRT";
import { MovableComponent } from "../components/MovableComponent";
import { BarrierComponent, PlayerComponent } from "../components/NoDataComponent";
import { PositionComponent } from "../components/PositionComponent";



export class BarrierSystem extends ECSSystem {
    
    constructor()
    {
        super();
    }

    start() 
    {
    }
    

    update()
    {
        var barrierComs =  this.ecs.components.all(BarrierComponent)
      
        barrierComs.forEach(element=>
        {
            if (element.enable == false)
            {
                return;
            }

            // Util.Log("barrierComs",barrierComs.length)
            var playerComponent = this.ecs.components.get(PlayerComponent)
            var playerEntity = this.ecs.entities.get(playerComponent.entityId);
            var barrierEntity = this.ecs.entities.get(element.entityId);
            var barrierPos = barrierEntity.getComponent(PositionComponent).pos;
            var playerPos = playerEntity.getComponent(PositionComponent).pos;

            if (Vector3.Distance(barrierPos,playerPos) < 0.2)
            {   
                Util.Log("碰撞了")
                element.enable = false;
                var movable = playerEntity.getComponent(MovableComponent)
                movable.resistance = movable.speed; //给阻力设置值
            }
        });
    }
}